package android.slc.appdb.sys.service.imp;

import android.slc.appdb.sys.entity.CsDictType;
import android.slc.appdb.sys.enums.StateEnum;
import android.slc.appdb.sys.service.CsDictTypeMapper;
import android.slc.appdb.utils.DictEnumUtils;
import android.slc.box.db.service.impl.DbEntityMapperImpl;
import cn.hutool.core.util.IdUtil;
import io.reactivex.rxjava3.annotations.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/8/13 14:29
 */
public class CsDictTypeMapperImp extends DbEntityMapperImpl<CsDictType> implements CsDictTypeMapper {
    public CsDictTypeMapperImp() {
        super("dictType");
    }

    public CsDictTypeMapperImp(String dbName) {
        super(dbName, "dictType");
    }

    @Override
    public Serializable put(@Nullable CsDictType entity) {
        return super.put(entity);
    }

    @Override
    public void checkEntity(CsDictType entity) {
        super.checkEntity(entity);
        if (entity.getDictId() == null) {
            entity.setDictId(IdUtil.getSnowflakeNextId());
        }
        CsDictType csDictType = findDictTypeByDictType(entity.getDictType());
        if (csDictType != null) {
            if (!Objects.equals(entity.getDictId(), csDictType.getDictId())) {
                throw new RuntimeException("字典类型" + entity.getDictType() + "已存在！");
            }
        }
        entity.setStatusName(DictEnumUtils.getLabelByValue(StateEnum.values(), entity.get_status()));
    }

    @Override
    public CsDictType findDictTypeByDictType(@NonNull String dictType) {
        return getOne((targetData, queryCondition) -> StringUtils.equals(targetData.getDictType(), dictType));
    }

    @Override
    public Map<String, CsDictType> getAllPrivate() {
        Map<String, CsDictType> map = super.getAllPrivate();
        map.forEach((s, csDictType) -> csDictType.setStatusName(DictEnumUtils.getLabelByValue(StateEnum.values(), csDictType.get_status())));
        return map;
    }
}
